package com.example.mymusic.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

import com.example.mymusic.utils.Music;

public class China_MBReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //获得Action
        String intentAction = intent.getAction() ;
        //获得KeyEvent对象
        KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

        Log.i("TAG", "Action ---->"+intentAction + "  KeyEvent----->"+event.toString());


        if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {

            if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) {//按下  up按键抬起，这里就监测按下就行了
                switch (event.getKeyCode()) {
                    case KeyEvent.KEYCODE_MEDIA_PLAY:
                        // 播放
                        Log.d("TAG", "onReceive: 播放");
                        Music.start();
                        break;
                    case KeyEvent.KEYCODE_MEDIA_PAUSE:
                        //暂停 按钮被按下
                        Log.d("TAG", "onReceive: 暂停");
                        Music.pause();
                        break;
                    case KeyEvent.KEYCODE_MEDIA_NEXT:
                        // 下一首 按钮被按下
                        Log.d("TAG", "onReceive: 下一首");
                        Music.playNext();
                        break;
                    case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                        Log.d("TAG", "onReceive: 上一首");
                        // 上一首 按钮被按下
                        Music.playPrevious();
                        break;
                    // 更多的按键事件可以在这里添加处理逻辑
                }
            }

        }
    }
}
